document.querySelector("#btn").addEventListener('click', function () {
    var n = document.querySelector("#n").value;
    console.log(n);
    var s = document.querySelector("#s").value;
    console.log(s);
    var a = document.querySelector("#a").value;
    console.log(a);
    //将调价到网页的东西通过ajax的方式添加到库里的东西
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
            console.log(xmlhttp.responseText);
            //将输入的东西添加到到网页
            var xmlhttp2;
            if (window.XMLHttpRequest) {
                xmlhttp2=new XMLHttpRequest();
            }else{
                xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp2.onreadystatechange=function() {
                if (xmlhttp2.readyState==4 && xmlhttp2.status==200) {
                    console.log(xmlhttp2.responseText);
                    var json = JSON.parse(xmlhttp2.responseText);
                    console.log(json.data);
                    json = json.data.reverse();
                    for (var i =0; i<json.length;i++){
                        var tr = document.createElement("tr");
                        var td1 = document.createElement("td");
                        var td2 = document.createElement("td");
                        var td3 = document.createElement("td");
                        tr.appendChild(td1);
                        tr.appendChild(td2);
                        tr.appendChild(td3);
                        td1.innerHTML = json[i].studentName;
                        td2.innerHTML = json[i].sex;
                        td3.innerHTML = json[i].age;
                        document.querySelector("#list").appendChild(tr);
                    }
                }
            };
            xmlhttp2.open("GET","http://localhost/ajax/public/index.php/index/student/student",true);
            xmlhttp2.send();
        }
    };
    xmlhttp.open("GET","http://localhost/ajax/public/index.php/index/student/addStudent?studentName= "+n+" &sex= "+s+" &age="+ a, true);
    xmlhttp.send();
    document.querySelector("#n").value = "";
    document.querySelector("#a").value = "";
    //将以上添加到网页的东西全部清空
    document.querySelector("#list").innerHTML ="";
});
//数据库内容，最后将所有数据库的东西调出来
var xmlhttp2;
if (window.XMLHttpRequest) {
    xmlhttp2=new XMLHttpRequest();
}else {
    xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp2.onreadystatechange=function() {
    if (xmlhttp2.readyState==4 && xmlhttp2.status==200) {
        console.log(xmlhttp2.responseText);
        var json = JSON.parse(xmlhttp2.responseText);
        console.log(json.data);
        json = json.data.reverse();
        for (var i =0; i<json.length;i++){
            var tr = document.createElement("tr");
            var td1 = document.createElement("td");
            var td2 = document.createElement("td");
            var td3 = document.createElement("td");
            tr.appendChild(td1);
            tr.appendChild(td2);
            tr.appendChild(td3);
            td1.innerHTML = json[i].studentName;
            td2.innerHTML = json[i].sex;
            td3.innerHTML = json[i].age;
            document.querySelector("#list").appendChild(tr);
        }
    }
};
xmlhttp2.open("GET","http://localhost/ajax/public/index.php/index/student/student",true);
xmlhttp2.send();